package com.lagou.controller;

import com.lagou.dao.PayDAO;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * @author: lcz
 * @date: Created in 9:55 2021/2/24
 * @description:
 */
@Controller
public class PayController {

    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private PayDAO payDAO;

    /**
     * @author: lcz
     * @date: Created in 13:12 2021/2/24
     * @description: 支付
     */
    @GetMapping("/pay")
    public String paySubmit(@RequestParam("money") String money, @RequestParam("bizId") String bizId,
                            Map<String, Object> map) {

        if (payDAO.validBiz(bizId)) {
            payDAO.set(bizId, money);
            map.put("list", payDAO.list());
            System.out.println("支付成功");
            payDAO.list().forEach(System.out::println);
            return "success";
        }
        map.put("list", payDAO.listTimeoutBiz());
        System.out.println("支付超时");
        payDAO.listTimeoutBiz().forEach(System.out::println);
        return "cancel";
    }

    /**
     * @author: lcz
     * @date: Created in 13:15 2021/2/24
     * @description: 下单
     */
    @GetMapping(value = "/toPay")
    public String pay(Map<String, String> map) {
        String value = UUID.randomUUID().toString();
        map.put("bizId", value);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(new Date());
        System.out.println("发送消息，time：" + time + "，bizId：" + value);
        amqpTemplate.convertAndSend("ex.pay.ttl", "pay.ttl", value);
        return "topay";
    }

    @GetMapping(value = "/index")
    public String toPay() {
        return "index";
    }
}
